<?php
namespace app\Admin\controller\post;

//引入公共控制器
use app\common\controller\Backend;

use think\Request;

class Post extends Backend
 {
    public function __construct() {

        //继承父类
        parent::__construct();

        $this->PostModel = Model( 'Post.Post' );
        $this->CommentModel = Model( 'Post.Comment' );

    }

    // 显示帖子列表

    public function index() {

        // 获取开始时间
        $start = $this->request->param( 'start', '' );

        // 获取结束时间
        $end = $this->request->param( 'end', '' );

        // 获取关键字
        $keywords = $this->request->param( 'keywords', '' );

        // 条件集合
        $where = [];

        $cateId = $this->request->param( 'cateid', '' );

        if ( !empty( $cateId ) ) {
            $where[ 'post.cateid' ] = $cateId;
        }

        if ( $start ) {
            // 加入条件 大于开始时间
            $where[ 'post.createtime' ] = [ '> time', $start ];
        }

        if ( $end ) {
            // 加入条件 小于或等于结束时间
            $where[ 'post.createtime' ] = [ '<= time', $end ];
        }

        // 若两个都存在就将两个条件拼接
        if ( $start && $end ) {
            $where[ 'post.createtime' ] = [ 'between time', [ $start, $end ] ];
        }

        if ( $keywords ) {
            // 加入条件 关键字
            $where[ 'title' ] = [ 'like', "%$keywords%" ];
        }

        // 查询帖子总数
        $count = $this->PostModel->count();

        $postlist = $this->PostModel->with( [ 'cate', 'user' ] )->where( $where )->order( 'id', 'desc' )->paginate( 10, false, [ 'query' => Request::instance()->param() ] );

        $pagestr = $postlist->render();

        $this->assign( [
            'count' => $count,
            'postlist' => $postlist,
            'pagestr' => $pagestr,
            'keywords' => $keywords,
            'start' => $start,
            'end' => $end,
        ] );

        return $this->fetch();
    }

    public function del() {
        if ( $this->request->isPost() ) {
            // 提示信息
            $success = [
                'result' => false,
                'msg' => '',
            ];

            $id = $this->request->param( 'ids', 0 );

            // 查询数据库是否有帖子
            $postInfo = $this->PostModel->find( $id );

            if ( !$postInfo ) {
                $this->error( '帖子不存在!' );
                exit;
            }

            // 前端传来的ID转换为数组
            $idarr = explode( ',', $id );

            foreach ( $idarr as $val ) {
                // 查询该帖子下的所有评论
                $comment = $this->CommentModel->where( [ 'postid' => $val ] )->select();

                // 评论ID
                $ids = [];

                foreach ( $comment as $item ) {
                    $ids[] = $item[ 'id' ];
                }

                // 评论表的软删除
                $res = $this->CommentModel->destroy( $ids );
            }

            // 帖子表的软删除
            $result = $this->PostModel->destroy( $id );

            if ( $result === false ) {
                $success[ 'result' ] = false;
                $success[ 'msg' ] = $this->ConfigModel->getError();
            } else {
                $success[ 'result' ] = true;
                $success[ 'msg' ] = '删除帖子成功！';
            }

            return $success;
        }
    }

    public function recycle() {
        // 获取开始时间
        $start = $this->request->param( 'start', '' );

        // 获取结束时间
        $end = $this->request->param( 'end', '' );

        // 获取关键字
        $keywords = $this->request->param( 'keywords', '' );

        // 条件集合
        $where = [];

        if ( $start ) {
            // 加入条件 大于开始时间
            $where[ 'createtime' ] = [ '> time', $start ];
        }

        if ( $end ) {
            // 加入条件 小于或等于结束时间
            $where[ 'createtime' ] = [ '<= time', $end ];
        }

        // 若两个都存在就将两个条件拼接
        if ( $start && $end ) {
            $where[ 'createtime' ] = [ 'between time', [ $start, $end ] ];
        }

        if ( $keywords ) {
            // 加入条件 关键字
            $where[ 'title' ] = [ 'like', "%$keywords%" ];
        }

        // 全部用户的数量
        $postAll = $this->PostModel->onlyTrashed()->select();

        $postCount = count( $postAll );

        // 获取全部用户
        $postList = $this->PostModel->onlyTrashed()->where( $where )->order( 'id', 'desc' )->paginate( 10, false, [ 'query' => Request::instance()->param() ] );

        // 获取分页显示
        $postListstr = $postList->render();

        $this->assign( [
            'postList' => $postList,
            'postListstr' => $postListstr,
            'postCount' => $postCount,
            'keywords' => $keywords,
            'start' => $start,
            'end' => $end,
        ] );
        return $this->fetch();
    }

    // 取消软删除 恢复正常

    public function recovery() {
        if ( $this->request->isPost() ) {
            // 提示信息
            $success = [
                'result' => false,
                'msg' => '',
            ];

            $ids = $this->request->param( 'ids', 0 );

            $where = 'id in('.$ids.')';

            $data = [
                'deletetime' => null,
            ];

            // 软删除
            $result = $this->PostModel->onlyTrashed()->where( $where )->update( [ 'deletetime' => NULL ] );

            if ( $result === FALSE ) {
                $success[ 'result' ] = false;
                $success[ 'msg' ] = $this->PostModel->getError();
            } else {
                $success[ 'result' ] = true;
                $success[ 'msg' ] = '恢复帖子成功';
            }

            return json( $success );
        }

    }

    // 真正删除

    public function cut() {
        if ( $this->request->isPost() ) {
            // 提示信息
            $success = [
                'result' => false,
                'msg' => '',
            ];

            $ids = $this->request->param( 'ids', 0 );

            $where = 'id in('.$ids.')';

            // 软删除
            $result = $this->PostModel->onlyTrashed()->where( $where )->delete();

            if ( $result === FALSE ) {
                $success[ 'result' ] = false;
                $success[ 'msg' ] = $this->PostModel->getError();
            } else {
                $success[ 'result' ] = true;
                $success[ 'msg' ] = '删除帖子成功';
            }

            return json( $success );
        }
    }
}